package com.we.risk.smsInfo;

import com.alibaba.fastjson.JSONObject;
import com.we.risk.smsInfo.utils.smsInfoTools;
import com.we.utils.CommonTimeTools;
import com.we.utils.ParamCheck;
import com.we.utils.RegexTools;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SmsInfoBasic {
    public static Logger LOG = LoggerFactory.getLogger(SmsInfoBasic.class);

    public long capTime;
    public int isWeekend;
    public int isWeekend2;
    public int isDaytime;
    public int isRead;
    public int isRecv;

    public String phoneNum;
    public String body_name;
    public long recvTime;

    public int isTelecom;
    public int isExpress;
    public int isService;
    public int isPayTool;

    public int isBank;
    public int isCrdt;
    public int isCashOut;
    public int isCashIn;
    public int isLoanRepay;
    public int isItntLend;
    public int isOd;
    public int isLoanRepayRemind;
    public int isLoanRepaySucc;
    public int isLoanRepayFail;
    public int isLoanApplySucc;

    public BigDecimal bankRemainAmt = null;
    public BigDecimal cashOutAmt = null;
    public BigDecimal cashInAmt = null;
    public BigDecimal crdtAmt = null;
    public BigDecimal crdtCosuAmt = null;

    public BigDecimal loanLineAmt = null;
    public BigDecimal loanRepayAmt = null;

    public static SmsInfoBasic constructSmsinfoBasic(
            JSONObject smsInfo, Long capTime, SimpleDateFormat sdf) {
        try {
            String phoneNum = smsInfo.getString("address");
            //            ParamCheck.isNull(phoneNum, "phoneNum");
            Integer type = smsInfo.getInteger("type");
            //            ParamCheck.isNull(type, "type");
            Integer isRead = smsInfo.getInteger("read");
            //            ParamCheck.isNull(isRead, "isRead");
            Long date = smsInfo.getLong("date");
            //            ParamCheck.isNull(date, "date");
            String body = smsInfo.getString("body");
            //            ParamCheck.isNull(body, "body");
            if (body != null) {
                body = body.replace("\n", " ").replace("\r", " ");
            }

            if ((type != null && (type == 1 || type == 2))
                    && (date != null && CommonTimeTools.dateDiff(capTime, date, sdf) <= 365)) {
                /** 注意type = 1 && body_name >= 2的限制 放到smsCategory处理 */
                int isDayTime = smsInfoTools.isDayTime(date);
                int isWeekend = smsInfoTools.isWeekend(date);
                int isWeekend2 = smsInfoTools.isWeekend2(date);
                String bodyName = smsInfoTools.getBodyName(body);
                if (type == 1 && bodyName != null && bodyName.length() >= 2) {

                    String trimBody = StringUtils.strip(body.replaceAll(",", ""), " ");

                    //                    String trimBody = StringUtils.strip(body.replaceAll(",",
                    // "").replaceAll("\\n",""), " ");
                    //                String trimBody = body.replaceAll(",", "").trim();
                    ParamCheck.isNull(trimBody, "trimBody");
                    //                    System.out.println("trimBody: " + trimBody);

                    int telecomFlag = smsInfoTools.getTelecomFlag(bodyName);
                    int expressFlag = smsInfoTools.getExpressFlag(bodyName);
                    int serviceFlag = smsInfoTools.getServiceFlag(bodyName);
                    int paytoolFlag = smsInfoTools.getPaytoolFlag(bodyName);

                    int bankFlag = smsInfoTools.getBankFlag(bodyName, trimBody);
                    int creditCardFlag = smsInfoTools.getCreditCardFlag(bodyName, trimBody);
                    int cashoutFlag = smsInfoTools.getcashOutFlag(trimBody);
                    int cashinFlag = smsInfoTools.getcashInFlag(trimBody);
                    int loanRepayFlag = smsInfoTools.getloanRepayFlag(trimBody);
                    BigDecimal bankRemainAmt = smsInfoTools.getBankRemainAmt(trimBody);
                    BigDecimal cashOutAmt = smsInfoTools.getCashOutAmt(trimBody);

                    BigDecimal cashInAmt = smsInfoTools.getCashInAmt(trimBody);
                    BigDecimal crdtAmt = smsInfoTools.getCrdtAmt(trimBody);
                    BigDecimal crdtCosuAmt = smsInfoTools.getCrdtCosuAmt(trimBody);

                    int itntLendFlag = smsInfoTools.getItntLendFlag(bodyName);
                    BigDecimal loanLineAmt = smsInfoTools.getLoanLineAmt(trimBody);
                    BigDecimal loanRepayAmt = smsInfoTools.getLoanRepayAmt(trimBody);
                    int odKwFlag = smsInfoTools.getOdKwFlag(trimBody);
                    int loanRepayRemindFlag = smsInfoTools.getLoanRepayRemindFlag(trimBody);
                    int loanRepaySuccessFlag = smsInfoTools.getLoanRepaySuccessFlag(trimBody);
                    int loanRepayFailFlag = smsInfoTools.getLoanRepayFailFlag(trimBody);
                    int loanApplySuccessFlag = smsInfoTools.getLoanApplySuccessFlag(trimBody);

                    return new SmsInfoBasic(
                            isWeekend,
                            isWeekend2,
                            isDayTime,
                            isRead,
                            type,
                            phoneNum,
                            bodyName,
                            date,
                            telecomFlag,
                            expressFlag,
                            serviceFlag,
                            paytoolFlag,
                            bankFlag,
                            creditCardFlag,
                            cashoutFlag,
                            cashinFlag,
                            loanRepayFlag,
                            itntLendFlag,
                            odKwFlag,
                            loanRepayRemindFlag,
                            loanRepaySuccessFlag,
                            loanRepayFailFlag,
                            loanApplySuccessFlag,
                            bankRemainAmt,
                            cashOutAmt,
                            cashInAmt,
                            crdtAmt,
                            crdtCosuAmt,
                            loanLineAmt,
                            loanRepayAmt,
                            capTime);

                } else {
                    return new SmsInfoBasic(
                            isWeekend,
                            isWeekend2,
                            isDayTime,
                            isRead,
                            type,
                            phoneNum,
                            bodyName,
                            date,
                            capTime);
                }
            }
        } catch (Exception e) {
            LOG.error("constructSmsinfoBasic excep: " + e.toString());
            LOG.error("constructSmsinfoBasic excep input: " + smsInfo);
        }
        return null;
    }

    public SmsInfoBasic(
            int isWeekend,
            int isWeekend2,
            int isDaytime,
            int isRead,
            int isRecv,
            String phoneNum,
            String body_name,
            long recvTime,
            long capTime) {
        this.isWeekend = isWeekend;
        this.isWeekend2 = isWeekend2;
        this.isDaytime = isDaytime;
        this.isRead = isRead;
        this.isRecv = isRecv;
        this.phoneNum = phoneNum;
        this.body_name = body_name;
        this.recvTime = recvTime;
        this.capTime = capTime;
    }

    public SmsInfoBasic(
            int isWeekend,
            int isWeekend2,
            int isDaytime,
            int isRead,
            int isRecv,
            String phoneNum,
            String body_name,
            long recvTime,
            int isTelecom,
            int isExpress,
            int isService,
            int isPayTool,
            int isBank,
            int isCrdt,
            int isCashOut,
            int isCashIn,
            int isLoanRepay,
            int isItntLend,
            int isOd,
            int isLoanRepayRemind,
            int isLoanRepaySucc,
            int isLoanRepayFail,
            int isLoanApplySucc,
            BigDecimal bankRemainAmt,
            BigDecimal cashOutAmt,
            BigDecimal cashInAmt,
            BigDecimal crdtAmt,
            BigDecimal crdtCosuAmt,
            BigDecimal loanLineAmt,
            BigDecimal loanRepayAmt,
            long capTime) {
        this.isWeekend = isWeekend;
        this.isWeekend2 = isWeekend2;
        this.isDaytime = isDaytime;
        this.isRead = isRead;
        this.isRecv = isRecv;
        this.phoneNum = phoneNum;
        this.body_name = body_name;
        this.recvTime = recvTime;
        this.isTelecom = isTelecom;
        this.isExpress = isExpress;
        this.isService = isService;
        this.isPayTool = isPayTool;
        this.isBank = isBank;
        this.isCrdt = isCrdt;
        this.isCashOut = isCashOut;
        this.isCashIn = isCashIn;
        this.isLoanRepay = isLoanRepay;
        this.isItntLend = isItntLend;
        this.isOd = isOd;
        this.isLoanRepayRemind = isLoanRepayRemind;
        this.isLoanRepaySucc = isLoanRepaySucc;
        this.isLoanRepayFail = isLoanRepayFail;
        this.isLoanApplySucc = isLoanApplySucc;
        this.bankRemainAmt = bankRemainAmt;
        this.cashOutAmt = cashOutAmt;
        this.cashInAmt = cashInAmt;
        this.crdtAmt = crdtAmt;
        this.crdtCosuAmt = crdtCosuAmt;
        this.loanLineAmt = loanLineAmt;
        this.loanRepayAmt = loanRepayAmt;
        this.capTime = capTime;
    }

    public long getCapTime() {
        return capTime;
    }

    public int getIsCashOut() {
        return isCashOut;
    }

    public int getIsCashIn() {
        return isCashIn;
    }

    public int getIsLoanRepay() {
        return isLoanRepay;
    }

    public int getIsOd() {
        return isOd;
    }

    public int getIsLoanRepayRemind() {
        return isLoanRepayRemind;
    }

    public int getIsLoanRepaySucc() {
        return isLoanRepaySucc;
    }

    public int getIsLoanRepayFail() {
        return isLoanRepayFail;
    }

    public int getIsLoanApplySucc() {
        return isLoanApplySucc;
    }

    public BigDecimal getBankRemainAmt() {
        return bankRemainAmt;
    }

    public BigDecimal getCashOutAmt() {
        return cashOutAmt;
    }

    public BigDecimal getCashInAmt() {
        return cashInAmt;
    }

    public BigDecimal getCrdtAmt() {
        return crdtAmt;
    }

    public BigDecimal getCrdtCosuAmt() {
        return crdtCosuAmt;
    }

    public BigDecimal getLoanLineAmt() {
        return loanLineAmt;
    }

    public BigDecimal getLoanRepayAmt() {
        return loanRepayAmt;
    }

    public int getIsWeekend() {
        return isWeekend;
    }

    public int getIsWeekend2() {
        return isWeekend2;
    }

    public int getIsDaytime() {
        return isDaytime;
    }

    public int getIsRead() {
        return isRead;
    }

    public int getIsRecv() {
        return isRecv;
    }

    public int getIsTelecom() {
        return isTelecom;
    }

    public int getIsExpress() {
        return isExpress;
    }

    public int getIsService() {
        return isService;
    }

    public int getIsPayTool() {
        return isPayTool;
    }

    public int getIsBank() {
        return isBank;
    }

    public int getIsCrdt() {
        return isCrdt;
    }

    public int getIsItntLend() {
        return isItntLend;
    }

    public String getBody_name() {
        return body_name;
    }

    public String getPhoneNum() {
        return phoneNum;
    }

    public long getRecvTime() {
        return recvTime;
    }

    @Override
    public String toString() {
        return "SmsInfoBasic{"
                + "isWeekend="
                + isWeekend
                + ", isDaytime="
                + isDaytime
                + ", isRead="
                + isRead
                + ", isRecv="
                + isRecv
                + ", phoneNum='"
                + (phoneNum == null ? "null" : phoneNum)
                + '\''
                + ", body_name='"
                + body_name
                + '\''
                + ", recvTime="
                + recvTime
                + ", isTelecom="
                + isTelecom
                + ", isExpress="
                + isExpress
                + ", isService="
                + isService
                + ", isPayTool="
                + isPayTool
                + ", isBank="
                + isBank
                + ", isCrdt="
                + isCrdt
                + ", isCashOut="
                + isCashOut
                + ", isCashIn="
                + isCashIn
                + ", isLoanRepay="
                + isLoanRepay
                + ", isItntLend="
                + isItntLend
                + ", isOd="
                + isOd
                + ", isLoanRepayRemind="
                + isLoanRepayRemind
                + ", isLoanRepaySucc="
                + isLoanRepaySucc
                + ", isLoanRepayFail="
                + isLoanRepayFail
                + ", isLoanApplySucc="
                + isLoanApplySucc
                + ", bankRemainAmt="
                + (bankRemainAmt == null ? "null" : bankRemainAmt)
                + ", cashOutAmt="
                + (cashOutAmt == null ? "null" : cashOutAmt)
                + ", cashInAmt="
                + (cashInAmt == null ? "null" : cashInAmt)
                + ", crdtAmt="
                + (crdtAmt == null ? "null" : crdtAmt)
                + ", crdtCosuAmt="
                + (crdtCosuAmt == null ? "null" : crdtCosuAmt)
                + ", loanLineAmt="
                + (loanLineAmt == null ? "null" : loanLineAmt)
                + ", loanRepayAmt="
                + (loanRepayAmt == null ? "null" : loanRepayAmt)
                + '}';
    }

    public static void main1(String[] args) {
        //        String input = "您尾号8653卡11月17日14:18POS支出(无卡支付)1元，余额1,368.36元。【工商银行】";
        String regex =
                "(?<=转出.{0,6}|交易人民币-.{0,6}|支付人民币.{0,6}|支出.{0,6}|支付.{0,6}|转帐付.{0,6}|向.{0,6}付款.{0,6}|"
                        + "向.{0,6}账户转账.{0,6}|取出.{0,6}|发生一笔消费.{0,6}|余额发生变动\\(-.{0,6}|支取.{0,6}|付款.{0,6}|取人民币.{0,6}|"
                        + "跨行汇款.{0,6}|给.{0,8}账户转账.{0,6}|行内转账交易.{0,6}|跨行转账交易.{0,6}|账户快捷金额.{0,6}|账户消费金额.{0,6}|"
                        + "账户微信转账金额.{0,6}|手机银行跨行转账.{0,6}|账户微信红包金额.{0,6}|向.{0,8}汇款.{0,6}|户银行转证券金额.{0,6}|"
                        + "账户代扣金额.{0,6}|账户消费金额.{0,6}|银联扣款人民币.{0,6}|实时转至他行人民币.{0,6}|向财付通完成财付通交易人民币.{0,6}|"
                        + "完成转支交易人民币.{0,6}|交易人民币-.{0,6}|取款.{0,6}|成功付款人民币.{0,6}|跨行转账.{0,6}|邮消金额.{0,6}|"
                        + "完成代付交易人民币.{0,6}|完成财付通交易人民币.{0,6}|账户扣款金额.{0,6})(\\d+\\.?\\d{0,3})";

        String input =
                "尊敬的客户：您支用的网商贷已于11月14日19:37成功发放至支付宝15636763553(*越)。温馨提示：勿向任何人透露个人信息，谨防诈骗，有疑问可联系官方客服 95188-3。【网商银行】";
        String bodyName = smsInfoTools.getBodyName(input);
        String trimBody = StringUtils.strip(input.replaceAll(",", ""), " ");
        int cashOutFlag = smsInfoTools.getcashOutFlag(trimBody);
        BigDecimal cashOutAmt = smsInfoTools.getCashOutAmt(trimBody);

        System.out.println("bodyName:" + bodyName);
        System.out.println("trimBody:" + trimBody);
        System.out.println("cashOutFlag: " + cashOutFlag);
        System.out.println("cashOutAmt: " + cashOutAmt);
    }

    public static void main(String[] args) {
        String pattern =
                "(?<=转出.{0,6}|交易人民币-.{0,6}|支付人民币.{0,6}|支出.{0,6}|支付.{0,6}|转帐付.{0,6}|向.{0,6}付款.{0,6}|"
                        + "向.{0,6}账户转账.{0,6}|取出.{0,6}|发生一笔消费.{0,6}|余额发生变动\\(-.{0,6}|支取.{0,6}|付款.{0,6}|取人民币.{0,6}|"
                        + "跨行汇款.{0,6}|给.{0,8}账户转账.{0,6}|行内转账交易.{0,6}|跨行转账交易.{0,6}|账户快捷金额.{0,6}|账户消费金额.{0,6}|"
                        + "账户微信转账金额.{0,6}|手机银行跨行转账.{0,6}|账户微信红包金额.{0,6}|向.{0,8}汇款.{0,6}|户银行转证券金额.{0,6}|"
                        + "账户代扣金额.{0,6}|账户消费金额.{0,6}|银联扣款人民币.{0,6}|实时转至他行人民币.{0,6}|向财付通完成财付通交易人民币.{0,6}|"
                        + "完成转支交易人民币.{0,6}|交易人民币-.{0,6}|取款.{0,6}|成功付款人民币.{0,6}|跨行转账.{0,6}|邮消金额.{0,6}|"
                        + "完成代付交易人民币.{0,6}|完成财付通交易人民币.{0,6}|账户扣款金额.{0,6})(\\d+\\.?\\d{0,3})";

        String remainAmtPattern = "(?<=余额.{0,5}|余)(\\d+\\.?\\d*)";
        String input =
                "尊敬的客户：您支用的网商贷已于11月14日19:37成功发放至支付宝15636763553(*越)。温馨提示：勿向任何人透露个人信息，谨防诈骗，有疑问可联系官方客服 95188-3。【网商银行】";
        String remainAmtInput =
                "【招商银行】招行银讯通-余额通知：账号710900382010888(湖北奶爸婴幼儿用品有限公司)，20221101的可用余额为人民币1.26。";
        String loanLinePattern =
                "(?<=额度.{1,5}|成功提额|已获取|预审批额度已于今日由|可借款.资格.{1,2}|预审批额度由|可借金额已提升为)(\\d+\\.?\\d+)";
        String loanLineInput =
                "【你我贷】您好，限时费用低至3.7折，急速放款，打开t.nwd.cn/QveMJn 点击领取额度！详询请拨4000627626，退订回T";
        String cashInPattern =
                "(?<=转入.{0,6}|收入.{0,6}|转账收.{0,6}|存入.{0,6}|存款.{0,6}|提现.{0,6}|入账.{0,6}|收到汇款人民币.{0,6}|完成转存交易.{0,6}|向您尾号.{0,8}账户完成网银转账.{0,6}|代发.{0,6}|冲正.{0,6}|到账.{0,6}|向您尾号.{0,8}账户他行来账金额.{0,6}|工资.{0,6}|完成现存交易人民币.{0,6}|完成往来交易人民币.{0,6})(\\d+\\.?\\d{0,3})";
        String cashinP =
                "(?<=转入.{0,6}|收入.{0,6}|转账收.{0,6}|存入.{0,6}|存款.{0,6}|提现.{0,6}|入账.{0,6}|收到汇款人民币.{0,6}|完成转存交易.{0,6}|向您尾号.{0,8}账户完成网银转账.{0,6}|代发.{0,6}|冲正.{0,6}|到账.{0,6}|向您尾号.{0,8}账户他行来账金额.{0,6}|工资.{0,6}|完成现存交易人民币.{0,6}|完成往来交易人民币.{0,6})(\\d+\\.?\\d{0,3})";
        String cashInput =
                "【广西北部湾银行】账号：2894 活期\n时间：11月8日21:09\n类型：存入\n金额：9200.00\n余额：9244.16\n存款保险保护您珍贵的存款";

        String cashInputFormat =
                " 【广西北部湾银行】账号：2894 活期\n"
                        + "时间：11月8日21:09\n"
                        + "类型：存入\n"
                        + "金额：9200.00\n"
                        + "余额：9244.16\n"
                        + "存款保险保护您珍贵的存款\n";
        String cashInputa = RegexTools.hiveUdfRegExReplace(cashInput, ",", "");
        System.out.println("cashInputb:" + cashInput);
        //        cashInput = cashInput.replaceAll("\\n", "");
        System.out.println("cashInputa:" + cashInputa);
        Pattern p = Pattern.compile(cashinP);
        Matcher matcher = p.matcher(cashInputa);
        if (matcher.find()) {
            MatchResult matchResult = matcher.toMatchResult();
            String res = matchResult.group(1);
            System.out.println("res: " + res);
            System.out.println("cmp: " + (Double.valueOf(res) > 99999999.99));
            if (res.length() > 10 || (Double.valueOf(res) > 99999999.99)) {
                System.out.println("lennght > 10, return null");
            }

        } else {
            System.out.println("res null");
        }
    }
}
